@extends('layouts.userctl')
@section('content')
<style>
	.layui-card-header {
		margin-top: 15px;
	}

	/*.layui-table-cell {
		display: flex;
		align-items: center;
	}*/

	.userstatus {
		margin-left: 6px;
		color: #888;
		font-size: 14px;
	}

	.layui-table-tips-c::before {
		position: absolute !important;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0px !important;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.laytable-cell-1-0-0 {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
{{csrf_field()}}
<div class="layuimini-container">
	<div class="layuimini-main">
		<div style="margin: 10px 10px 10px 10px" id="btn">
			<form class="layui-form layui-form-pane" action="">
				<div class="layui-form-item">
					<div class="layui-inline" style="float: right;">
						<!--注意此处button标签里的type属性-->
						<button type="button" class="layui-btn layui-btn-normal" lay-submit id="searchbtn"
							lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"></i>搜  索</button>
					</div>
					<div class="layui-inline" style="float: right;">
						<label class="layui-form-label">关键字</label>
						<div class="layui-input-inline">
							<!--注意此处input标签里的id-->
							<input class="layui-input" name="keyword" id="keywordinput">
						</div>
					</div>
					<div class="layui-inline" style="float: left;">
						<!-- <a class="layui-btn layui-btn-normal" href="{{route('admin.msg.addmsg')}}"
							style="color: #fff;"><i class="layui-icon layui-icon-addition"></i>发送消息</a> -->
						<a class="layui-btn layui-btn-normal" href="{{route('admin.msg.addmsg2')}}"
								style="color: #fff;"><i class="layui-icon layui-icon-reply-fill"></i>发送消息</a>
					</div>
				</div>
			</form>
		</div>
		<!--注意此处table标签里的id-->
		<table class="layui-table layui-hide" id="dataTable" lay-filter="dataTable"></table>
	</div>
</div>

<!-- <div class="layui-card">
	<div class="layui-card-header layuiadmin-card-header-auto">
		<a class="layui-btn layui-btn-normal" href="{{route('admin.user.adduser')}}" style="color: #fff;">新增用户</a>
	</div>
	<div class="layui-card-body">
		<table id="dataTable" lay-filter="dataTable"></table>
	</div>
</div> -->

<script>
	layui.use(['layer', 'table', 'form'], function() {
		var layer = layui.layer
	var form = layui.form
	var table = layui.table
	//用户表格初始化
	var dataTable = table.render({
			elem: '#dataTable',
			height: 'full',
			limit: 12,
			url: '{{ route("admin.msg.data") }}',
			page: true,
			cols: [
				[ //表头
					{ field: 'id', title: '序 号' },
					{ field: 'title', title: '消息标题' }, 
					{ field: 'content', title: '消息内容',width:'25%' }, 
					// { field: 'attachment', title: '附件' },
					{ field: 'add_time', title: '发送时间' },
					// { field: 'file_name', title: '文件名' },
					{ field: 'from_person', title: '发送人' },
					{ field: 'to_person', title: '接收人',width:'20%' },
					{
						field: 'msgstate',
						width:'22%',
						title: '消息状态',
						templet: function(d) {
							if(d.msgstate.length==2){
								return '<button class="layui-btn layui-btn-primary layui-border-green layui-btn-xs">'+d.msgstate+'</button>'
							}else{
								return '<button class="layui-btn layui-btn-primary layui-border-red layui-btn-xs">'+d.msgstate+'</button>'
							}
						},
					} 
				]
			],
			done: function(response) { //定位当前table视图
				//动态监听表头高度变化，冻结行跟着改变高度
				$('.layui-table-header  tr').resize(function() {
					$('.layui-table-header  tr').each(function(index, val) {
						$($('.layui-table-fixed .layui-table-header table tr')[index])
							.height($(val).height())
				})
			})
			//初始化高度，使得冻结行表头高度一致
			$('.layui-table-header  tr').each(function(index, val) {
					$($('.layui-table-fixed .layui-table-header table tr')[index]).height($(
						val).height())
			})

			//表体部分
			//动态监听表体高度变化，冻结行跟着改变高度
			$('.layui-table-body  tr').resize(function() {
					$('.layui-table-body  tr').each(function(index, val) {
						$($('.layui-table-fixed .layui-table-body table tr')[index])
							.height($(val).height())
				})
			})
			//初始化高度，使得冻结行表体高度一致
			$('.layui-table-body  tr').each(function(index, val) {
					$($('.layui-table-fixed .layui-table-body table tr')[index]).height($(val)
						.height())
			})
			var tableView = this.elem.next()

			//response.data获取后端返  回的JSON格式数据
			$.each(response.data, function(index, item) {

					//逻辑判断
					if (item.is_delete == '0') {
						//改变TR标签内字体颜色
						tableView.find('tbody tr[data-index="' + index + '"]').css('color',
							'#FF0033')
				}
				})
			}
		})
	$('#searchbtn').click(function() {
			dataTable.reload({
				where: { keyword: $('#keywordinput').val() },
				page: { curr: 1 }
			})
	})
		form.on('switch(delctl)', function(data) {
			var id = data.elem.getAttribute('data-id')
			var value = data.elem.checked
			$.ajax({
				type: 'POST',
				headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
				url: '/admin/user/delctl',
				data: { id, value },
				success: function(res) {
					if (res.code == '1') {
						if (value) {
							layer.msg('启用成功', { icon: 6 })
						$(data.othis).next().html('启用')
						} else {
							layer.msg('禁用成功', { icon: 6 })
						$(data.othis).next().html('禁用')
						}
					}
				}
			})
	})
	//监听工具条
	table.on('tool(dataTable)', function(obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
			var data = obj.data //获得当前行数据
				,
				layEvent = obj.event //获得 lay-event 对应的值
		if (layEvent === 'reset') {
				layer.confirm('确认重置密码吗', { btn: ['确认', '取消'] }, function(index, layero) {
					var id = data.id
					$.ajax({
						type: 'POST',
						headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').val() },
						url: '/admin/user/resetpwd',
						data: { id },
						success: function(res) {
							if (res.code == '1') {
								layer.msg(res.msg, { icon: 6 })
							}
						}
					})
			}, function(index) {

				})
		} else if (layEvent === 'edit') {
				location.href = '/admin/user/useredit?id=' + data.id
			}
		})
})
</script>
@endsection
